package exp1;
import exp3.userdata;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.*;
import java.util.Map;
import java.util.Optional;


@WebServlet(urlPatterns = {"/login/dgut"})

public class dgut extends HttpServlet{
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        int flag=1;
        LoginController login=new LoginController();
        Map<String,String> map=login.login(request,response);
        HttpSession session=request.getSession();
        String username = map.get("username");
        String name = map.get("name");
        String group = map.get("group");
        String openid1=map.get("openid1");
        String wx_openid=map.get("wx_openid");
        session.setAttribute("username",username);
        session.setAttribute("name",name);
        session.setAttribute("group",group);
        session.setAttribute("openid1",openid1);
        session.setAttribute("wx_openid",wx_openid);

        StringBuilder result = new StringBuilder();
        File file = new File("informaion.txt");
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (Exception e) {
                System.out.println("error to create");
                e.printStackTrace();
            }
        }
        BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
        String s = null;String B;
        while((s = br.readLine())!=null){//使用readLine方法，一次读一行
            result.append(s);
            B=result.toString();
            String BB[]=B.split(" ");
            if(BB[0].equals(username))
            {
               flag=0;
            }
            result.delete(0,result.length());
        }
        if(flag==1)
        {
            FileWriter fileWritter = new FileWriter(file.getName(), true);
            fileWritter.write(username + " "+name+" "+ "123456" + " " + "1092650656@qq.com"+" "+group +"\n");
            fileWritter.close();
        }
        System.out.println("111");
        request.getRequestDispatcher("/display.jsp").forward(request,response);
    }
}
